package com.ch.clinking.adapter.Order;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.bean.OrderInfo;

import java.util.List;

public class AddOrderAdapter extends RecyclerView.Adapter<AddOrderAdapter.MyViewHolder> {

    public List<OrderInfo> orderInfoList;
    private LayoutInflater layoutInflater;
    private Activity context;
    private Integer operation = 0;
    public Integer batch = 0;
    public Integer changeOperateNum = 1;

    public AddOrderAdapter(Activity context, List<OrderInfo> orderInfoList) {
        this.orderInfoList = orderInfoList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }


    @NonNull
    @Override

    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.list_add_order_sku_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public int getItemCount() {
        return orderInfoList.size();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        OrderInfo orderInfo = orderInfoList.get(position);
        Glide.with(context).load(OkHttpUtil.getInstance().getIMG_URL()+orderInfo.getImage()).into(holder.image);
        holder.title.setText(orderInfo.getTitle());
        holder.addNum.setText(Integer.toString(orderInfo.getFinalNum()));

        // 设置默认占位图片
        Glide.with(context)
                .load(R.drawable.appicon)
                .into(holder.image);

        // 加载图片
        Glide.with(context)
                .load(OkHttpUtil.getInstance().getIMG_URL() + orderInfo.getImage())
                .placeholder(R.drawable.appicon)
                .diskCacheStrategy(DiskCacheStrategy.ALL)  // 缓存策略：缓存原始图片和转换后的图片
                .thumbnail(0.5f)  // 缩略图大小
                .error(R.drawable.appicon)
                .into(holder.image);

        // 清除旧的TextWatcher
        if (holder.addNum.getTag() instanceof TextWatcher) {
            holder.addNum.removeTextChangedListener((TextWatcher) holder.addNum.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable editable) {
                String c = editable.toString();
                if (c.equals("")) {
                    c = "0";
                }
                int change = Integer.parseInt(c);
                orderInfo.setFinalNum(change);
            }
        };

        holder.addNum.addTextChangedListener(watcher);
        holder.addNum.setTag(watcher);

        holder.toZero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.addNum.setText("0");
            }
        });

    }

    @SuppressLint("NotifyDataSetChanged")
    public void flash() {
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView title;
        EditText addNum;
        Button toZero;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            this.image = itemView.findViewById(R.id.add_order_sku_img);
            this.title = itemView.findViewById(R.id.add_order_sku_title);
            this.addNum = itemView.findViewById(R.id.add_order_sku_final_num);
            this.toZero = itemView.findViewById(R.id.add_oder_to_zero);


        }
    }

}
